package tq.com.tqteacher.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.githang.statusbar.StatusBarCompat;

import pub.devrel.easypermissions.EasyPermissions;
import tq.com.tqcom.base.BaseActivity;
import tq.com.tqcom.util.TQEventBus;
import tq.com.tqcom.view.TQClickButton;
import tq.com.tqim.config.TqConfig;
import tq.com.tqim.net.TQImRequest;
import tq.com.tqteacher.R;
import tq.com.tqteacher.activity.im.QTImLogin;
import tq.com.tqteacher.activity.login.TQLoginActivity;
import tq.com.tqteacher.adapt.TQHomePublishAdapter;
import tq.com.tqteacher.adapt.TQLaunchMainAdapter;
import tq.com.tqteacher.fragment.TQHomePublishFragment;
import tq.com.tqteacher.fragment.TQNewPublishFragment;
import tq.com.tqteacher.inter.INewPulibshListener;
import tq.com.tqteacher.model.TQNewPublish;
import tq.com.tqteacher.model.TQPublishItem;
import tq.com.tqteacher.model.TQPublishSection;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.view.TQHeader3ViewHold;


public class LaunchActivity extends BaseActivity {
    private TQLaunchMainAdapter mainAdapter = null;
    private ViewPager viewPager;

    static String TAG = "LaunchActivity";

    private int currentSelected = 0;
    private TQClickButton[] clickButtons = new TQClickButton[5];
    private TQPublishSection parentId = null;


    static public void show(Activity base){
        Intent intent = new Intent(base, LaunchActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
        base.startActivity(intent);
        base.finish();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);

        setStatusBar();
        StatusBarCompat.setLightStatusBar(getWindow(), true);

        viewPager = findViewById(R.id.tq_content);
        if(mainAdapter == null){
            mainAdapter = new TQLaunchMainAdapter(getSupportFragmentManager(), mOpenNewPublishListener);
        }
        viewPager.setAdapter(mainAdapter);
        viewPager.addOnPageChangeListener(onPageChangeListener);

        clickButtons[0] = findViewById(R.id.tq_0);
        clickButtons[1] = findViewById(R.id.tq_1);
        clickButtons[2] = findViewById(R.id.tq_2);
        clickButtons[3] = findViewById(R.id.tq_3);
        clickButtons[4] = findViewById(R.id.tq_4);

        currentSelected = 0;
        int i = 0;
        for (TQClickButton clickButton : clickButtons){
            clickButton.setOnClickListener(onClickListener);
            clickButton.setIndex(i++);
        }

        requestPromission();
    }



    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {
            Log.d(TAG, "onPageScrolled: ");
        }

        @Override
        public void onPageSelected(int i) {
            Log.d(TAG, "onPageSelected: ");
            if(i == currentSelected) return;

            clickButtons[i].setSelected(true);
            clickButtons[currentSelected].setSelected(false);
            currentSelected = i;

        }

        @Override
        public void onPageScrollStateChanged(int i) {
            Log.d(TAG, "onPageScrollStateChanged: ");
        }
    };

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(v instanceof TQClickButton){
                int index = ((TQClickButton)v).getIndex();
                if(index == currentSelected) return;

                ((TQClickButton)v).setSelected(true);
                clickButtons[currentSelected].setSelected(false);
                currentSelected = index;
                viewPager.setCurrentItem(currentSelected, true);

            }
        }
    };


    private TQNewPublishFragment newPublishFragment = null;


    private TQNewPublishFragment.Callback publishCallback = new TQNewPublishFragment.Callback() {
        @Override
        public void onCancle() {
            if(newPublishFragment.isAdded() && !newPublishFragment.isHidden()){
                FragmentManager fragmentManager = LaunchActivity.this.getSupportFragmentManager();
                fragmentManager.beginTransaction().hide(newPublishFragment).commit();
            }
        }

        @Override
        public void onPublish(int type) {

            TQPublishType.setTQOpenFrom(TQPublishType.TQOpenFrom.home);
            TQPublishType.genNewPublishByType(type);
            int lId = 0;
            if(parentId != null){
                lId = parentId.getLastItem().getNewPublish().getSeq_id();
            }

            TQPublishType.getSharePublishItem().setParentId(lId);

            TQPublishType.showPublishNextStep(LaunchActivity.this);

            onCancle();
        }
    };

    private void requestPromission(){
        String[] permission = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.RECORD_AUDIO};
        if(!EasyPermissions.hasPermissions(this, permission)){
            EasyPermissions.requestPermissions(this, "班群需要使用您的录音功能", 0x01, permission);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }



    private INewPulibshListener mOpenNewPublishListener = new INewPulibshListener() {
        @Override
        public void newPublish() {
            parentId = null;
            TQHomePublishFragment lPublishFragment = (TQHomePublishFragment)mainAdapter.getItem(0);
            if(lPublishFragment != null){
                lPublishFragment.cancelLinkNewPublish();
            }
            openNewPublish();
        }

        @Override
        public void newListPublish(TQPublishSection pPublish) {
            Log.d(TAG, "newListPublish: ");
            if(pPublish != null){
                parentId = pPublish;
            }

            openNewPublish();
        }
    };

    private void openNewPublish(){
        if(newPublishFragment == null){
            newPublishFragment = new TQNewPublishFragment();
            newPublishFragment.setCallback(publishCallback);
        }
        FragmentManager fragmentManager = LaunchActivity.this.getSupportFragmentManager();
        if(newPublishFragment.isAdded()){
            fragmentManager.beginTransaction().show(newPublishFragment).commit();
        }else{
            fragmentManager.beginTransaction().add(R.id.new_publish, newPublishFragment).commit();
        }
    }



}
